﻿using Newtonsoft.Json.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace AccumulateApp.Control
{
    /// <summary>
    /// Interaction logic for JumpSlider.xaml
    /// </summary>
    public partial class JumpSlider : System.Windows.Controls.UserControl
    {
        private Border _valueTick; // 用于显示当前值的刻度线

        public JumpSlider()
        {
            InitializeComponent();
        }

        //添加两个字符串类型的依赖属性，左侧标题和右侧标题
        public static readonly DependencyProperty LeftTitleProperty =
            DependencyProperty.Register("LeftTitle", typeof(string), typeof(JumpSlider), new PropertyMetadata(string.Empty));
        public static readonly DependencyProperty RightTitleProperty =
            DependencyProperty.Register("RightTitle", typeof(string), typeof(JumpSlider), new PropertyMetadata(string.Empty));
        public string LeftTitle
        {
            get { return (string)GetValue(LeftTitleProperty); }
            set { SetValue(LeftTitleProperty, value); }
        }

        public string RightTitle
        {
            get { return (string)GetValue(RightTitleProperty); }
            set { SetValue(RightTitleProperty, value); }
        }

        //添加4个int类型的依赖属性，最小刻度，最大刻度, 当前值, 刻度间隔TickFrequency
        public static readonly DependencyProperty MinValueProperty =
            DependencyProperty.Register("MinValue", typeof(int), typeof(JumpSlider), new PropertyMetadata(0));
        public static readonly DependencyProperty MaxValueProperty =
            DependencyProperty.Register("MaxValue", typeof(int), typeof(JumpSlider), new PropertyMetadata(100));
        public static readonly DependencyProperty CurrentValueProperty =
            DependencyProperty.Register("CurrentValue", typeof(int), typeof(JumpSlider), new PropertyMetadata(60));
        public static readonly DependencyProperty TickFrequencyProperty =
            DependencyProperty.Register("TickFrequency", typeof(int), typeof(JumpSlider), new PropertyMetadata(10));

        public int MinValue
        {
            get { return (int)GetValue(MinValueProperty); }
            set { SetValue(MinValueProperty, value); }
        }
        public int MaxValue
        {
            get { return (int)GetValue(MaxValueProperty); }
            set { SetValue(MaxValueProperty, value); }
        }
        public int CurrentValue
        {
            get { return (int)GetValue(CurrentValueProperty); }
            set { SetValue(CurrentValueProperty, value); }
        }
        public int TickFrequency
        {
            get { return (int)GetValue(TickFrequencyProperty); }
            set { SetValue(TickFrequencyProperty, value); }
        }


        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            // 设置默认值
            // 如果未设置当前值，则设置为最小值
            if (CurrentValue < MinValue || CurrentValue > MaxValue)
            {
                CurrentValue = MinValue;
            }
            left.Text = LeftTitle;
            right.Text = RightTitle;
            value.Text = value.Text;

            GenerateTicks();
        }

        private void GenerateTicks()
        {
            TickPanel.Children.Clear();

            // 添加列, 首尾列宽占1*， 其余列宽占2*
            TickPanel.ColumnDefinitions.Clear();
            for (int i = MinValue; i <= MaxValue; i += TickFrequency)
            {
                if (i == MinValue || i == MaxValue)
                {
                    TickPanel.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
                }
                else
                {
                    TickPanel.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(2, GridUnitType.Star) });
                }
            }

            //添加两行 40 像素的行定义， 和auto 行高
            TickPanel.RowDefinitions.Clear();
            TickPanel.RowDefinitions.Add(new RowDefinition { Height = new GridLength(40) });
            TickPanel.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });


            // 添加刻度线， 并设置其位置
            var style = (Style)FindResource("TickButton");
            for (int i = MinValue; i <= MaxValue; i += TickFrequency)
            {
                var column = i / TickFrequency; // 计算当前刻度的列索引
                // 添加按钮，按钮的样式从用UserControl.Resources 中获取样式 TickButton
                var button = new System.Windows.Controls.Button
                {
                    Style = style
                };
                // <TextBlock x:Name="left" Text="最小值"/> 添加刻度文本
                var textBlock = new TextBlock
                {
                    Text = i.ToString(),
                    HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
                    VerticalAlignment = System.Windows.VerticalAlignment.Center
                };
                if (i == MinValue)
                {
                    button.BorderThickness = new Thickness(1, 0, 0, 1);
                    button.Margin = new Thickness(-4, 10, 0, 0); // 左侧按钮不需要右边距
                    button.Padding = new Thickness(-4, 0, 0, 0); // 设置按钮的Padding
                    textBlock.HorizontalAlignment = System.Windows.HorizontalAlignment.Left; // 左侧按钮文本左对齐
                }
                else if (i == MaxValue)
                {
                    button.BorderThickness = new Thickness(0, 0, 1, 1);
                    button.Margin = new Thickness(0, 10, -4, 0); // 右侧按钮不需要左边距
                    button.Padding = new Thickness(0, 0, -4, 0); // 设置按钮的Padding
                    textBlock.HorizontalAlignment = System.Windows.HorizontalAlignment.Right; // 右侧按钮文本右对齐
                }

                // 添加点击事件处理程序
                button.Click += Button_Click;
                // 将按钮添加到 TickPanel
                Grid.SetColumn(button, column);
                TickPanel.Children.Add(button);
                // 将文本添加到 TickPanel 第二行
                Grid.SetRow(textBlock, 1);
                Grid.SetColumn(textBlock, column);
                TickPanel.Children.Add(textBlock);

                if (i > MinValue && i < MaxValue)
                {
                    if ((MaxValue - MinValue) != 2 * i)
                    {
                        var tick = new Border
                        {
                            Background = System.Windows.Media.Brushes.DarkOrange,
                            Width = 1,
                            HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
                            Margin = new Thickness(0, 10, 0, 0) // 上边距10像素
                        };
                        Grid.SetColumn(tick, column);
                        TickPanel.Children.Add(tick);
                    }
                }
            }

            // 额外添加一个中间的刻度线，高度大于其他刻度线
            // 创建一个中间的刻度线
            var middleTick = new Border
            {
                Background = System.Windows.Media.Brushes.Green,
                Width = 2,
                HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
                Margin = new Thickness(0, 0, 0, 0) // 上边距0像素
            };
            // 设置 Grid.ColumnSpan
            middleTick.SetValue(Grid.ColumnSpanProperty, TickPanel.ColumnDefinitions.Count);
            TickPanel.Children.Add(middleTick);

            // 创建 valueTick，显示当前值, 红色， 高度大于其他刻度线，小于中间刻度线
            _valueTick = new Border
            {
                Background = System.Windows.Media.Brushes.Red,
                Width = 2,
                HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
                Margin = new Thickness(0, 5, 0, 0) // 上边距10像素
            };

            SetValue((CurrentValue - MinValue)/TickFrequency);
            TickPanel.Children.Add(_valueTick);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // 找到当前按钮所在的 Grid.Column
            var button = sender as System.Windows.Controls.Button;
            if (button != null)
            {
                int columnIndex = Grid.GetColumn(button);
                SetValue(columnIndex);
            }
        }

        private void SetValue(int columnIndex)
        {
            Grid.SetColumn(_valueTick, columnIndex);
            if (columnIndex == 0)
            {
                _valueTick.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            }
            else if (columnIndex == TickPanel.ColumnDefinitions.Count - 1)
            {
                _valueTick.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            }
            else
            {
                _valueTick.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            }

            // 更新当前值
            CurrentValue = MinValue + columnIndex * TickFrequency; // 假设按钮的列索引对应于值
            value.Text = CurrentValue.ToString();
        }
    }
}
